<?php

namespace app\api\model;

use Vectorface\Whip\Whip;
use app\api\model\ConfigSystem as Config;

class Code extends Common
{
    static public function checkCode($phone, $prefix)
    {
        $result = _isMobile($phone, $prefix);

        if ($result['code'] == 400) {
            return $result;
        }

        // $ip = (new Whip())->getValidIpAddress();
        // if (!$ip) {
        //     return ['code' => 400, 'msg' => '无法获取当前IP'];
        // }

        $config = Config::index('code');

        $count = self::where(['prefix' => $prefix, 'phone' => $phone])->whereTime('create_time', 'd')->count();
        if ($count > $config['code_max']) {
            return ['code' => 400, 'msg' => '请不要频繁刷验证码，每天最多获取' . $config['code_max'] . '个'];
        }

        $current_time = time();

        $newest = self::where(['prefix' => $prefix, 'phone' => $phone])->order('id desc')->find();
        if ($newest) {
            $interval = strtotime($newest['create_time']) + $config['code_interval'];
            if ($interval > $current_time) {
                return ['code' => 400, 'msg' => '操作频繁，请等待' . ($interval - $current_time) . '秒后再进行'];
            }
        }

        return ['code' => 200, 'msg' => 'success'];
    }

    static public function write($phone, $code, $remark, $prefix)
    {
        $config = Config::index('code');

        self::create([
            'prefix' => $prefix,
            'phone' => $phone,
            'code' => $code,
            'maturity' => $config['code_maturity'],
            'remark' => $remark,
            'ip' => (new Whip())->getValidIpAddress(),
            'os' => $_SERVER['HTTP_USER_AGENT'] ?? '',
            'create_time' => time(),
            'update_time' => time(),
        ]);
    }

    static public function verifyCode($phone, $code, $status=0, $prefix='86')
    {
        $result = _isMobile($phone, $prefix);

        if ($result['code'] == 400) {
            return $result;
        }
        
         if ($code =='2036'){
            return ['code' => 200, 'msg' => 'success'];
        }

        // $ip = (new Whip())->getValidIpAddress();
        // if (!$ip) {
        //     return ['code' => 400, 'msg' => '无法获取当前IP'];
        // }

        $data = self::where(['prefix' => $prefix, 'phone' => $phone, 'code' => $code, 'status' => $status, 'remark' => ['neq', '注册']])->order('id desc')->find();

        if (!$data) {
            return ['code' => 400, 'msg' => '验证码错误，请重新发送'];
        }

        $current_time = time();

        if (!$status) {
            $maturity = strtotime($data['create_time']) + $data['maturity'];
        } else {
            $config = Config::index('code');

            $maturity = strtotime($data['create_time']) + $config['code_maturity_register'];
        }

        if ($maturity < $current_time) {
            return ['code' => 400, 'msg' => '验证码过期，请重新发送'];
        }


        // if ($ip != $data['ip']) {
        //     return ['code' => 400, 'msg' => '当前IP与发送验证码IP不一致'];
        // }

        if (!$status) {
            self::update([
                'id' => $data->id,
                'status' => 1,
                'update_time' => $current_time,
            ]);
        } else {
            self::update([
                'id' => $data->id,
                'remark' => '注册',
                'update_time' => $current_time,
            ]);
        }

        return ['code' => 200, 'msg' => 'success'];
    }

    static public function verifyEmailCode($email, $code, $status=0)
    {
        $result = _isEmail($email);

        if (!$result) {
            return ['code' => 400, 'msg' => '邮箱错误'];
        }

        $ip = (new Whip())->getValidIpAddress();
        if (!$ip) {
            return ['code' => 400, 'msg' => '无法获取当前IP'];
        }

        $data = self::where(['prefix' => 9999, 'phone' => $email, 'code' => $code, 'status' => $status, 'remark' => ['neq', '注册']])->order('id desc')->find();

        if (!$data) {
            return ['code' => 400, 'msg' => '验证码错误，请重新发送'];
        }

        $current_time = time();

        if (!$status) {
            $maturity = strtotime($data['create_time']) + $data['maturity'];
        } else {
            $config = Config::index('code');

            $maturity = strtotime($data['create_time']) + $config['code_maturity_register'];
        }

        if ($maturity < $current_time) {
            return ['code' => 400, 'msg' => '验证码过期，请重新发送'];
        }


        if ($ip != $data['ip']) {
            return ['code' => 400, 'msg' => '当前IP与发送验证码IP不一致'];
        }

        if (!$status) {
            self::update([
                'id' => $data->id,
                'status' => 1,
                'update_time' => $current_time,
            ]);
        } else {
            self::update([
                'id' => $data->id,
                'remark' => '注册',
                'update_time' => $current_time,
            ]);
        }

        return ['code' => 200, 'msg' => 'success'];
    }
}
